package com.szholly.data.general.metadata.table;

import java.util.HashMap;

import org.springframework.stereotype.Component;

import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.*;
import com.szholly.data.general.metadata.IBaseTable;

/**
 * 类功能说明 DATA_VERSION业务逻辑类
 */
@Component("DATA_VERSION")
public class VersionTable extends InterceptorTableBase  implements IBaseTable{

	public final static String TABLE_NAME = "DATA_VERSION";
	
	/**
	 * 获取单例对象
	 */
	public static VersionTable getSingleRef() {
		return (VersionTable) SpringBeanFactory.getBean("DATA_VERSION");
	}

	@Override
	public String getMetadataTableName() {
		return TABLE_NAME;
	}

	@Override
	public String getMetadataTableAliasName() {
		return "数据库版本表";
	}
	
	@Override
	public String getCompanyName(){
		return null;
	}

	private HashMap<String, Field> fields;

	@Override
	protected HashMap<String, Field> fields() {
		if (fields == null) {
			fields = new HashMap<String, Field>();

			Field itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_GUID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(1);
			itemField.setFieldAliasName("全局ID");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_VERSION);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("数据版本");
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_COMPANY);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("企业名称");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_SYSTEMNAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("系统名称");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(VersionRow.FIELD_SYSVERSION1);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("主版本");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields
					.put(itemField.getFieldName().toUpperCase().trim(),
							itemField);

			itemField = new Field();
			itemField.setFieldName(VersionRow.FIELD_SYSVERSION2);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("子版本2");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields
					.put(itemField.getFieldName().toUpperCase().trim(),
							itemField);

			itemField = new Field();
			itemField.setFieldName(VersionRow.FIELD_SYSVERSION3);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("子版本3");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields
					.put(itemField.getFieldName().toUpperCase().trim(),
							itemField);

			itemField = new Field();
			itemField.setFieldName(VersionRow.FIELD_SYSVERSION4);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(5);
			itemField.setFieldAliasName("子版本4");
			itemField.setCanShow(true);
			itemField.setAllowNull(true);
			fields
					.put(itemField.getFieldName().toUpperCase().trim(),
							itemField);

		}
		return fields;
	}

	@Override
	public String getTableGroup() {
		return "Data框架";
	}

	/**
	 * 版本处理，当前数据库版本
	 */
	public final int SysVersion1 = 3;
	public final int SysVersion2 = 2;
	public final int SysVersion3 = 0;
	public final int SysVersion4 = 0;
	public final String CurrentDataBaseVersion = "3.2.0.0";

	private String dataVersion;

	/**
	 * 获取库当前版本
	 */
	public String GetDataBaseVersion() {
		if (StringUtils.IsNullOrSpace(dataVersion)) {
			VersionRow row = (VersionRow) this.getEntity(VersionRow.class, new QueryFilter());
			if (row != null)
				dataVersion = row.getSysVersion1() + "." + row.getSysVersion2()
						+ "." + row.getSysVersion3() + "."
						+ row.getSysVersion4();
			else {
				row = new VersionRow();
				row.setSysVersion1(SysVersion1);
				row.setSysVersion2(SysVersion2);
				row.setSysVersion3(SysVersion3);
				row.setSysVersion4(SysVersion4);
				this.save(row);
				dataVersion = row.getSysVersion1() + "." + row.getSysVersion2()
						+ "." + row.getSysVersion3() + "."
						+ row.getSysVersion4();
			}
		}
		return dataVersion;
	}

	/**
	 * 获取当前的数据库是否和系统数据库版本同步
	 */
	public boolean GetDataBaseVersionIsSyncVer() {
		String strVer = GetDataBaseVersion();
		return strVer.equals(CurrentDataBaseVersion);
	}
}
